package edu.biwu.cookie01;

import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;

/**
 * 如何获取浏览器携带的Cookie
 *  我们可以在Request中找到获取Cookie方法
 *      public Cookie[] getCookies():获取浏览器发送过来的所有Cookie对象，如果浏览器没有发送任何cookie，该方法返回null
 *      public void getName():获取cookie中封装的name
 *      public void getValue():获取cookie中封装的value
 */
@WebServlet("/cookie/receive")
public class ReceiveCookie extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1.获取浏览器携带的所有Cookie
        Cookie[] cookies = request.getCookies();

        //2.遍历cookie数组，从中取出所有cookie
        if (cookies != null){
            for (Cookie cookie : cookies) {
                System.out.println(cookie.getName() + "=" + cookie.getValue());
            }
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
